package com.four.dailymanage.chamber.service;

import com.four.dailymanage.chamber.dao.ChamberDao;
import com.four.dailymanage.chamber.entity.Chamber;
import com.four.dailymanage.common.bean.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChamberService {
    @Autowired
    ChamberDao chamberDao;

    public  boolean addChamber(Chamber chamber){
        if (chamber==null){
            return false;
        }
        System.out.println(chamber);
        
        return chamberDao.insertChamber(chamber)>0;
    }
    public boolean delChamber(Integer id){
        if (id == 0 || id <0){
            return  false;
        }
        return chamberDao.deleteChamber(id)>0;
    }

    public  boolean updChamber(Chamber chamber){
        if (chamber == null){
            return false;
        }
        return chamberDao.updateChamber(chamber)>0;
    }

    public Page<Chamber> getPageChamber(int pageNum,int pageSize,String cname,String cno,String status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Chamber> list=chamberDao.getChamberByCon(start,pageSize,cname,cno,status);
        //获取总条目数
        int total=chamberDao.countChamber(cname, cno,status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Chamber> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

}
